#include "ESCells.h"
#include <sstream>
#include "util.h"

using namespace std;

ESCell::ESCell(std::string column, std::string value, bool canDivide) :
    column(std::move(column)), value(std::move(value)), canDivide(canDivide) { }

ESRow::ESRow(std::string  rowKey, std::initializer_list<ESCell> cells) : Key(std::move(rowKey)), Cells(cells) { }

ESCells::ESCells(std::string indexName, std::initializer_list<ESRow> rows) : IndexName(std::move(indexName)), Rows(rows) { }

std::string ESCells::ToString() const
{
    string body;
    for (auto& row : Rows)
    {
        for (auto &cell: row.Cells)
        {
            stringstream ss;
            ss << R"({"index":{"_index":")";
            ss << IndexName;
            ss << R"(","_type":"_doc","_id":")";
            ss << Base64Encode(cell.column);
            ss << "@";
            ss << Base64Encode(row.Key);
            ss << R"("}})";
            ss << "\n";
            ss << R"({"row_key":")";
            ss << row.Key;
            ss << R"(","column":")";
            ss << cell.column;
            ss << R"(",)";
            // TODO: How to save divide info, overload with timestamp
            ss << (cell.canDivide ? R"("value_t":")" : R"("value_k":")");
            ss << cell.value;
            ss << R"("})";
            ss << "\n";
            body += ss.str();
        }
    }
    return body;
}

std::string ESCells::ToString(const string & indexName) const
{
    string body;
    for (auto& row : Rows)
    {
        for (auto &cell: row.Cells)
        {
            stringstream ss;
            ss << R"({"index":{"_index":")";
            ss << indexName;
            ss << R"(","_type":"_doc","_id":")";
            ss << Base64Encode(cell.column);
            ss << "@";
            ss << Base64Encode(row.Key);
            ss << R"("}})";
            ss << "\n";
            ss << R"({"row_key":")";
            ss << row.Key;
            ss << R"(","column":")";
            ss << cell.column;
            ss << R"(",)";
            // TODO: How to save divide info, overload with timestamp
            ss << (cell.canDivide ? R"("value_t":")" : R"("value_k":")");
            ss << cell.value;
            ss << R"("})";
            ss << "\n";
            body += ss.str();
        }
    }
    return body;
}


